package com.clss.webrtclibrary;

import android.content.Context;
import android.content.Intent;
import android.opengl.GLSurfaceView;
import com.clss.webrtclibrary.Lg;

import org.webrtc.EglBase;
import org.webrtc.MediaStream;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.RendererCommon;
import org.webrtc.SurfaceViewRenderer;

import static com.clss.webrtclibrary.KurentoServer.WEBRTC_TYPE_SEND;

/**
 * 仅发布视频房间
 * @author liuv
 */
public class SendVideoRoom {
    private static final String TAG = "SendVideoRoom";

    private MediaInfoBean mMediaInfoBean;
    private Context mContext;
    private KurentoServer mKurentoServer;
    private GLSurfaceView mGlSurfaceView;
    private MediaStream mLocalStream;
    private IVideoRoomListener mIVideoRoomListener;
    private EglBase mEglBase;
    private SurfaceViewRenderer mFullRenderer;
    private ProxyVideoSink mFullProxyVideoSink = new ProxyVideoSink();
    private KurentoMediaCallback mKurentoMediaCallback;

    public SendVideoRoom(MediaInfoBean mMediaInfoBean, SurfaceViewRenderer fullRenderer,
                         IVideoRoomListener IVideoRoomListener) {
        this.mMediaInfoBean = mMediaInfoBean;
        this.mIVideoRoomListener = IVideoRoomListener;
        this.mContext = mIVideoRoomListener.getContext();
        this.mEglBase = EglBase.create();
        this.mFullRenderer = fullRenderer;

        mFullRenderer.init(mEglBase.getEglBaseContext(), null);
        mFullRenderer.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FILL);
        mFullRenderer.setEnableHardwareScaler(true);

        mFullProxyVideoSink.setTarget(mFullRenderer);
        startVideo();
    }

    private void startVideo(){
        mKurentoServer = new KurentoServer(mContext, WEBRTC_TYPE_SEND,
                new KurentoGatewayCallback());
        PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
        mKurentoServer.initializeMediaContext(options);
    }

    public boolean stopVideo(){
        if (mFullProxyVideoSink != null) {
            mFullProxyVideoSink.setTarget(null);
        }

        if (mFullRenderer != null){
            mFullRenderer.release();
            mFullRenderer = null;
        }

        mFullProxyVideoSink = null;
//        mEglBase.releaseSurface();
        mEglBase.release();
        return mKurentoServer.closeAllLiveVideo();
    }

    public boolean switchCamera(){
        return mKurentoServer.switchCamera();
    }


    private void videoStart(){
        if (mIVideoRoomListener != null){
            mIVideoRoomListener.videoOnStart();
        }
    }

    /** 收到新的信令 */
    public void receivedNewSign(KurentoSignallingBean receivedBean){
        if (mKurentoServer != null){
            mKurentoServer.receivedNewMessage(receivedBean);
        }
    }

    /**
     * 网关回调
     */
    private class KurentoGatewayCallback implements IKurentoGatewayCallback {

        @Override
        public MediaInfoBean getMediaInfo() {
            return mMediaInfoBean;
        }

        @Override
        public IKurentoMediaCallbacks getMediaHandleCallback() {
            return mKurentoMediaCallback == null ?
                    mKurentoMediaCallback = new KurentoMediaCallback()
                    : mKurentoMediaCallback;
        }

        @Override
        public EglBase getEglBase() {
            return mEglBase;
        }

        @Override
        public IVideoRoomListener getVideoRoomListener() {
            return mIVideoRoomListener;
        }

        @Override
        public void webSocketDisconnect() {
            Lg.i(TAG, "---webSocketDisconnect===");
            if (mIVideoRoomListener != null){
                mIVideoRoomListener.videoRoomDisconnect();
            }
        }

        @Override
        public void signalingParamError(String error) {
            if (mIVideoRoomListener != null){
                mIVideoRoomListener.videoError(error);
            }
        }

        @Override
        public void arrivedRoom(KurentoSignallingBean signallingBean) {

        }

        @Override
        public void leaveRoom(String id) {

        }

        @Override
        public void showConnectionInfo(String info) {

        }

        @Override
        public Boolean getVideoTrackEnable() {
            return true;
        }
    }

    /**
     * media回调
     */
    public class KurentoMediaCallback implements IKurentoMediaCallbacks {

        @Override
        public void success(KurentoPeerManager handle) {

        }

        @Override
        public void onLocalStream(MediaStream stream) {
            Lg.i(TAG, "---onLocalStream===");
            videoStart();
            mLocalStream = stream;
            stream.videoTracks.get(0).addSink(mFullProxyVideoSink);
        }

        @Override
        public void onRemoteStream(MediaStream stream, String id) {
            Lg.i(TAG, "---onRemoteStream===" + id);
        }

        @Override
        public void onRemoveRemoteStream(String id) {

        }

        @Override
        public void onDetached() {

        }

        @Override
        public Intent getScreenIntent() {
            return null;
        }

        @Override
        public void enableVideo(String id) {
            Lg.i(TAG, "---enableVideo===" + id);
        }

        @Override
        public void disableVideo(String id) {
            Lg.i(TAG, "---disableVideo===" + id);
        }
    }
}
